package com.deloitte.sapnetweaver.command
{
	import com.adobe.cairngorm.control.CairngormEvent;
	import com.deloitte.sapnetweaver.events.FileDownLoadEvent;
	
	import flash.events.Event;
	import flash.events.IOErrorEvent;
	import flash.events.SecurityErrorEvent;
	import flash.net.FileReference;
	import flash.net.URLRequest;
	import flash.net.URLRequestMethod;
	
	import mx.controls.Alert;

	public class FileDownloadCommand extends BaseGetDataCommand
	{
		[Bindable]private  var fr:FileReference = new FileReference();
		override public  function execute(event:CairngormEvent):void {
			fr.addEventListener(Event.COMPLETE,completeHandler);
			fr.addEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler);
			fr.addEventListener(SecurityErrorEvent.SECURITY_ERROR, securityErrorHandler);
			
			var downloadURLRequest:URLRequest = new URLRequest();            
			downloadURLRequest.method = URLRequestMethod.POST;
			downloadURLRequest.url = FileDownLoadEvent(event).urlToDownLoad; 
			
			
			try
			{	        		            
				fr.download(downloadURLRequest);
			}
			catch (error:Error)
			{
				trace("Unable to download files");
			}
		}
		private function completeHandler(event:Event):void 
		{
			Alert.show("Download Complete","Download Success");
		}
		private function ioErrorHandler(event:IOErrorEvent):void 
		{
			trace("ioErrorHandler: " + event);
			Alert.show("Error..."+event,"Error");
		}
		
		private function securityErrorHandler(event:SecurityErrorEvent):void 
		{
			trace("securityErrorHandler: " + event);
			Alert.show("SecurityError..."+event,"Security Error");
		}
	}
}